<?php

namespace App\Http\Requests\Api;


class UserRequest extends FormRequest
{
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules = [
            'profile.name' => 'required',
            'roles' => 'required|array',
            'profile.gender' => 'boolean',
            'is_active' => 'boolean'
        ];
        switch ($this->method()) {
            case 'PATCH':
                $user = $this->route('user');
                $id = $user->id;
                $rules['name'] = 'between:3,25|regex:/^[A-Za-z0-9\-\_]+$/|unique:users,name,' . $id;
                $rules['phone'] = 'required|regex:/^1[345789]\d{9}$/|unique:users,phone,' . $id;
                break;
            case 'POST':
                $rules['name'] = 'between:3,25|regex:/^[A-Za-z0-9\-\_]+$/|unique:users';
                $rules['phone'] = 'required|regex:/^1[345789]\d{9}$/|unique:users';
                break;
        }
        return $rules;
    }

    public function messages()
    {
        return [
            'name.unique' => '用户名 已存在',
            'name.min' => '用户名长度只能在3-25个字符之间',
            'roles.required' => '角色不能为空',
            'roles.array' => '角色必须提交数组',
        ];
    }
}
